package com.example.javaeekeshe.config;

import com.example.javaeekeshe.pojo.AccountBean;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AccountBean loginUser = (AccountBean) request.getSession().getAttribute("loginUser");
        String servletPath = request.getServletPath();
        String substring = "";
        if (servletPath.length()>6){
            substring = servletPath.substring(0, 6);
        }

        if (substring!=null&&substring.equals("/admin")&&loginUser!=null&&loginUser.getLeave()==null){
            System.out.println("==>admin拦截");
            request.setAttribute("msg","需要管理员权限");
            request.getRequestDispatcher("/admin/login").forward(request,response);
            return false;
        }
        if (loginUser==null){
            System.out.println("==>拦截");
            System.out.println(servletPath);
            request.setAttribute("msg","没有权限请先登录");
            request.getRequestDispatcher("/login.html").forward(request,response);
            return false;
        }
        else {
            return true;
        }
    }
}
